#include "personnel.h"

#include <QSqlError>
#include <QSqlDatabase>
#include <QSqlQuery>

#include <QDebug>
#include <QMessageBox>

#include <QDateEdit>

/// If ignore=true throught change made to cell(used when retriving records)
bool ignore=false;

Personnel::Personnel()
{
    setupUi(this);

    //persoTable->setColumnWidth(0,100);
    //persoTable->setColumnWidth(1,400);
    /// we enbale sorting
    persoTable->setSortingEnabled(true);
}


void Personnel::setMode(bool mode){
    ignore=true;
}


void Personnel::addAbs() {

    persoTable->insertRow( persoTable->rowCount());
}

void Personnel::remAbs() {
    QMessageBox msgBox;
    msgBox.setWindowTitle("Confirmation");
    msgBox.setText("Voulez vous vraiment supprimer cet employé?");
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::No);
    int ret=msgBox.exec();
    if (ret==QMessageBox::Yes){
        persoTable->removeRow( persoTable->rowCount()-1);
    }
    //if (persoTable->rowCount()==0)
    //curRow=-1;
}

/// FIXME: do we need those ones

void Personnel::addAbs(QTableWidget *persoT) {

    persoT->insertRow( persoT->rowCount());
}

void Personnel::remAbs(QTableWidget *persoT) {
    QMessageBox msgBox;
    msgBox.setText("Confirmation");
    msgBox.setInformativeText("Voulez vous vraiment supprimer cet employé?");
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::No);
    int ret=msgBox.exec();
    if (ret){
        persoT->removeRow( persoT->rowCount()-1);
    }
    //if (persoTable->rowCount()==0)
    //curRow=-1;
}

void Personnel::clearTable(QTableWidget *persoT) {
    for (int i=0;i<persoT->rowCount();i++){
        persoT->removeRow(i);
    }
}



void Personnel::retrive(QTableWidget *persoT){
    QTableWidgetItem *newItem;
    //persoTable->clear();
    /// To avoid adding unnessary rows we start from the
    for (int i=persoT->rowCount();i<cin.size();i++){
        addAbs(persoT);
    }

    for (int i=0;i<cin.size();i++){


        newItem = new QTableWidgetItem(IDList.at(i),0);
        if (!ignore)
            newItem->setFlags(Qt::ItemIsSelectable);
        persoT->setItem(i, 0, newItem);

        newItem = new QTableWidgetItem(nom.at(i),0);
        if (!ignore)
            newItem->setFlags(Qt::ItemIsSelectable);
        persoT->setItem(i, 1, newItem);

        newItem = new QTableWidgetItem(prenom.at(i),0);
        if (!ignore)
            newItem->setFlags(Qt::ItemIsSelectable);
        persoT->setItem(i, 2, newItem);

        newItem = new QTableWidgetItem(cnss.at(i),0);
        if (!ignore)
            newItem->setFlags(Qt::ItemIsSelectable);
        persoT->setItem(i, 3, newItem);

        newItem = new QTableWidgetItem(cin.at(i),0);
        /// cin fields can't be edited once SET
        //if (!ignore)
        newItem->setFlags(Qt::ItemIsSelectable);
        persoT->setItem(i, 4, newItem);

        newItem = new QTableWidgetItem(salaire.at(i),0);
        if (!ignore)
            newItem->setFlags(Qt::ItemIsSelectable);
        persoT->setItem(i, 5, newItem);

        newItem = new QTableWidgetItem(poste.at(i),0);
        if (!ignore)
            newItem->setFlags(Qt::ItemIsSelectable);
        persoT->setItem(i, 6, newItem);

        newItem = new QTableWidgetItem(dateNais.at(i),0);
        if (!ignore)
            newItem->setFlags(Qt::ItemIsSelectable);
        persoT->setItem(i, 7, newItem);
        // QDateEdit dat=QDateEdit(dateNais.at(i));
        // persoT-> setCellWidget(i, 6,new QDateEdit) ;
        //(QDateEdit)persoT-> cellWidget(i, 6)->setDate(3,3,4);
        //persoTable->item(i,0)->setText(dates.at(i));
        //persoTable->item(i,1)->setText(raisons.at(i));
    }
}


void Personnel::showPerso(QTableWidget *persoT,QString field){

    QSqlQuery query("SELECT * FROM Employ order by "+field);

    clearTable(persoT);

    nom.clear();
    prenom.clear();
    cin.clear();
    cnss.clear();
    salaire.clear();
    poste.clear();
    dateNais.clear();
    IDList.clear();

    while (query.next()) {
        nom.append(query.value(0).toString());
        prenom.append(query.value(1).toString());
        cnss.append(query.value(2).toString());
        cin.append(query.value(3).toString());
        salaire.append(query.value(4).toString());
        poste.append(query.value(5).toString());
        /// ADD gender Comn support
        dateNais.append(query.value(7).toString());
        IDList.append(query.value(8).toString());
    }
    retrive(persoT);
}

void Personnel::showPerso(){

    QSqlQuery query("SELECT * FROM Employ order by nom");

    clearTable(persoTable);

    nom.clear();
    prenom.clear();
    cin.clear();
    cnss.clear();
    salaire.clear();
    poste.clear();
    dateNais.clear();
    IDList.clear();


    while (query.next()) {
        nom.append(query.value(0).toString());
        prenom.append(query.value(1).toString());
        cin.append(query.value(2).toString());
        cnss.append(query.value(3).toString());
        salaire.append(query.value(4).toString());
        poste.append(query.value(5).toString());
        dateNais.append(query.value(7).toString());
        IDList.append(query.value(8).toString());
    }
    retrive(persoTable);
}
